﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.Farmings_NextWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.Farmings_NextWay
{
    public class FirstNextWayCommandHandler : IRequestHandler<FirstNextWayCommand, ApiResult<Farmings_Next>>
    {
        IBaseReponsitory<Farmings_Next> Farmings_Next;

        public FirstNextWayCommandHandler(IBaseReponsitory<Farmings_Next> Farmings_Next)
        {
            this.Farmings_Next = Farmings_Next;
        }

        public async Task<ApiResult<Farmings_Next>> Handle(FirstNextWayCommand request, CancellationToken cancellationToken)
        {
       var Bachlist = await Farmings_Next.GetQuery().FirstOrDefaultAsync(c => c.Rabit_Batch == request.Batch);
            if (Bachlist != null)
            {
                return new ApiResult<Farmings_Next>
                {
                    Code = ApiEnum.Success,
                    Message = "加载成功了",
                    Data = Bachlist
                };
            }
            else 
            {
                return new ApiResult<Farmings_Next>
                {
                    Code = ApiEnum.Success,
                    Message = "请设置信息",
                };
            }
        }
    }
}
